package com.kx.yooli.kx.wechat.utils.date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author john_sqwen-childe.wen
 * date 2024/4/8
 * PROJECT_NAME wechat
 * description create DateTimeUtil by john_sqwen
 * and  value is
 */
public class DateTimeUtil {
    private final Logger log = LoggerFactory.getLogger(DateTimeUtil.class);
    public static Timestamp nowTimestamp()
    {
        return getTimestamp(System.currentTimeMillis());
    }

    public static Timestamp getTimestamp(long time) {
        return new Timestamp(time);
    }

    public static Date nowDate() {
        return new Date();
    }

    public static int getYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(1);
    }

    public static String formatDate(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        if (pattern == null) {
            pattern = "yyyy-MM-dd";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static String formatDateTime(Date date) {
        return formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String formatDateTime() {
        return formatDate(now(), "yyyy-MM-dd HH:mm:ss");
    }

    public static String formatDate(Date date) {
        return formatDate(date, "yyyy-MM-dd");
    }

    public static String formatDate(Date date, boolean flag) {
        return formatDate(date, "yyyyMMdd");
    }

    public static String formatDate() {
        return formatDate(now(), "yyyy-MM-dd");
    }

    public static String formatTime(Date date) {
        return formatDate(date, "HH:mm:ss");
    }

    public static String formatTime() {
        return formatDate(now(), "HH:mm:ss");
    }

    public static String formatTime2() {
        return formatDate(now(), "HHmmss");
    }

    public static Date now() {
        return new Date();
    }

    public static Date parseDateTime(String datetime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if ((datetime == null) || (datetime.equals("")))
            return null;
        try {
            return formatter.parse(datetime);
        } catch (ParseException localParseException) {
        }
        return parseDate(datetime);
    }

    public static Date parseDate(String date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        if ((date == null) || (date.equals("")))
            return null;
        try {
            return formatter.parse(date);
        } catch (ParseException localParseException) {
        }
        return null;
    }

    public static Date parseDate(Date datetime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        if (datetime == null)
            return null;
        try {
            return formatter.parse(formatter.format(datetime));
        } catch (ParseException localParseException) {
        }
        return null;
    }

    public static Date parseDateYYMMDD(String date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");

        if ((date == null) || (date.equals("")))
            return null;
        try {
            return formatter.parse(date);
        } catch (ParseException localParseException) {
        }
        return null;
    }

    public static String formatDate(Object o) {
        if (o == null) {
            return "";
        }
        if (o.getClass() == String.class)
            return formatDate((String)o);
        if (o.getClass() == Date.class)
            return formatDate((Date)o);
        if (o.getClass() == Timestamp.class) {
            return formatDate(new Date(((Timestamp)o).getTime()));
        }
        return o.toString();
    }

    public static String formatDateTime(Object o) {
        if (o.getClass() == String.class)
            return formatDateTime((String)o);
        if (o.getClass() == Date.class)
            return formatDateTime((Date)o);
        if (o.getClass() == Timestamp.class) {
            return formatDateTime(new Date(((Timestamp)o).getTime()));
        }
        return o.toString();
    }

    public static Date add(Date date, int field, int amount) {
        if (date == null) {
            date = new Date();
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(field, amount);

        return cal.getTime();
    }

    public static Date addMilliSecond(Date date, int amount) {
        return add(date, 14, amount);
    }

    public static Date addSecond(Date date, int amount) {
        return add(date, 13, amount);
    }

    public static Date addMiunte(Date date, int amount) {
        return add(date, 12, amount);
    }

    public static Date addHour(Date date, int amount) {
        return add(date, 10, amount);
    }

    public static Date addDay(Date date, int amount) {
        return add(date, 5, amount);
    }

    public static Date addMonth(Date date, int amount) {
        return add(date, 2, amount);
    }

    public static Date addYear(Date date, int amount) {
        return add(date, 1, amount);
    }

    public static Date getDate() {
        return parseDate(formatDate());
    }

    public static Date getDateTime() {
        return parseDateTime(formatDateTime());
    }

    public static Date parseDate(String date, boolean flag) {
        if (flag) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");

            if ((date == null) || (date.equals("")))
                return null;
            try {
                Date a = formatter.parse(date);
                return a;
            } catch (ParseException e) {
                return null;
            }
        }

        return parseDate(date);
    }
}
